package com.taxi.driver.module.order.price;

import com.qianxx.utils.RxUtil;
import com.socks.library.KLog;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.config.ConfigRepository;
import com.taxi.driver.data.order.OrderRepository;
import com.taxi.driver.data.user.UserRepository;

import javax.inject.Inject;

/**
 * 功能描述：
 */
public class PriceDetailPresenter extends BasePresenter implements PriceDetailContract.Presenter {

    private PriceDetailContract.View mView;
    private OrderRepository mOrderRepository;
    private UserRepository mUserRepository;
    private ConfigRepository mConfigRepository;
    private String mOrderUuid;

    @Inject
    public PriceDetailPresenter(PriceDetailContract.View view, OrderRepository orderRepository,
                                UserRepository userRepository, ConfigRepository configRepository) {
        mView = view;
        mOrderRepository = orderRepository;
        mUserRepository = userRepository;
        mConfigRepository = configRepository;
    }

    @Override
    public void setOrderUuid(String orderUuid) {
        mOrderUuid = orderUuid;
    }

    @Override
    public String getOrderUuid() {
        return mOrderUuid;
    }

    /**
     * (专车)获取实时价格
     */
    @Override
    public void getRealtimeFare() {
        mSubscriptions.add(mOrderRepository.getRealtimeFare(mOrderUuid)
                .compose(RxUtil.applySchedulers())
                .subscribe(entity -> {
                    KLog.e("RealtimeFare", mOrderUuid);
                    mView.setDisplay(entity);
                }, ex -> {
                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                    KLog.e("RealtimeFare", ex.getLocalizedMessage() + "," + mOrderUuid);
                }));
    }

    /**
     * (专车)获取费用明细
     */
    @Override
    public void reqOrderFare() {
        mSubscriptions.add(mOrderRepository.orderFare(getOrderUuid())
                .compose(RxUtil.applySchedulers())
                .subscribe(entity -> {
                    mView.setDisplay(entity);
                    KLog.e("order", getOrderUuid() + "");
                }, ex -> {
                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                    KLog.e("order", ex.getLocalizedMessage() + "");
                }));
    }

    /**
     * (出租车)获取费用明细
     */
    @Override
    public void reqFareItems() {
        mSubscriptions.add(
                mOrderRepository.reqFareItems(mOrderUuid)
                        .compose(RxUtil.applySchedulers())
                        .subscribe(entity -> mView.setDisplay(entity),
                                ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository)));
    }

    @Override
    public String getPriceRules() {
        return mConfigRepository.getPriceRules();
    }
}
